package com.ruoyi.carousel.controller;

import com.ruoyi.carousel.domain.XlkjCarousel;
import com.ruoyi.carousel.service.IXlkjCarouselService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图Controller
 *
 * @author wnag
 * @date 2022-01-14
 */
@RestController
@RequestMapping("/h5/carousel/carousel")
public class XlkjCarouselControllerH5 extends BaseController
{
    @Autowired
    private IXlkjCarouselService xlkjCarouselService;

    /**
     * 查询轮播图列表
     */
//    @PreAuthorize("@ss.hasPermi('carousel:carousel:list')")
    @GetMapping("/list")
    public TableDataInfo list(XlkjCarousel xlkjCarousel)
    {
        startPage();
        List<XlkjCarousel> list = xlkjCarouselService.selectXlkjCarouselList(xlkjCarousel);
        for(XlkjCarousel carousel:list){
            carousel.setImg(UrlUtils.getUrl() + carousel.getImg());
        }
        return getDataTable(list);
    }

    /**
     * 导出轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('carousel:carousel:export')")
    @Log(title = "轮播图", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjCarousel xlkjCarousel)
    {
        List<XlkjCarousel> list = xlkjCarouselService.selectXlkjCarouselList(xlkjCarousel);
        ExcelUtil<XlkjCarousel> util = new ExcelUtil<XlkjCarousel>(XlkjCarousel.class);
        return util.exportExcel(list, "轮播图数据");
    }

    /**
     * 获取轮播图详细信息
     */
    @PreAuthorize("@ss.hasPermi('carousel:carousel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(xlkjCarouselService.selectXlkjCarouselById(id));
    }

    /**
     * 新增轮播图
     */
    @PreAuthorize("@ss.hasPermi('carousel:carousel:add')")
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XlkjCarousel xlkjCarousel)
    {
        return toAjax(xlkjCarouselService.insertXlkjCarousel(xlkjCarousel));
    }

    /**
     * 修改轮播图
     */
    @PreAuthorize("@ss.hasPermi('carousel:carousel:edit')")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjCarousel xlkjCarousel)
    {
        return toAjax(xlkjCarouselService.updateXlkjCarousel(xlkjCarousel));
    }

    /**
     * 删除轮播图
     */
    @PreAuthorize("@ss.hasPermi('carousel:carousel:remove')")
    @Log(title = "轮播图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjCarouselService.deleteXlkjCarouselByIds(ids));
    }
}
